﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using ImagesDal;
using System.IO;
using System.Xml.Linq;
using System.Threading;


namespace MyAlbum.Data.Xml
{
    public class AlbumRepository : XmlRepository, IAlbumRepository
    {
        private static object locker = "locker";
        public AlbumRepository(string rootPath)
            : base(rootPath, "Albums")
        {
        }

        public int CountItems(bool visible)
        {
            return this.Document.Descendants("Album").Where(a => visible ? (bool)a.Element("Visible") : true).Count();
        }

        public List<MyAlbum.Model.Album> GetItems()
        {
            return GetItems(true);
        }
        public List<MyAlbum.Model.Album> GetItems(bool visible)
        {
            return this.Document.Descendants("Album").Where(a => visible ? (bool)a.Element("Visible") : true).Select(s => new MyAlbum.Model.Album()
            {
                Id = (s.Attribute("Id").Value),
                Name = s.Element("Name").Value.Trim(),
                ParentId = (s.Element("ParentId").Value),
                Timestamp = (DateTime)s.Element("Timestamp"),
                Visible = (bool)s.Element("Visible"),
                Url = s.Element("Url").Value.Trim(),
                Description = s.Element("Description").Value.Trim()
            }).ToList();
        }
        public void AddItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
                album.Id = Guid.NewGuid().ToString();
                lock (locker)
                {
                    XElement a = new XElement("Album",
                        new XAttribute("Id", album.Id),
                        new XElement("Visible", album.Visible),
                        new XElement("Timestamp", album.Timestamp),
                        new XElement("ParentId", album.ParentId),
                        new XElement("Name", album.Name),
                        new XElement("Url", album.Url),
                        new XElement("Description", album.Description));
                    Document.Element("Albums").Add(a);
                    this.Save();
                }
        }

        public void UpdateItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
            Assert.IsNotNull(album.Name, "album name");
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Album") where e.Attribute("Id").Value == album.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Element("Visible").ReplaceWith(new XElement("Visible", album.Visible));
                    saved.Element("ParentId").ReplaceWith(new XElement("ParentId", album.ParentId));
                    saved.Element("Name").ReplaceWith(new XElement("Name", album.Name));
                    saved.Element("Url").ReplaceWith(new XElement("Url", album.Url));
                    saved.Element("Description").ReplaceWith(new XElement("Description", album.Description));
                    this.Save();
                }
            }
        }

        public void DeleteItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Album") where e.Attribute("Id").Value == album.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Remove();
                    this.Save();
                }
            }
        }


        public MyAlbum.Model.Album GetItemById(string albumId)
        {
            return Document.Descendants("Album")
                .Where(a => a.Attribute("Id").Value == albumId)
                .Select(
                a => new MyAlbum.Model.Album()
                    {
                        Description = a.Element("Description").Value.Trim(),
                        Id = a.Attribute("Id").Value,
                        Name = a.Element("Name").Value.Trim(),
                        ParentId = a.Element("ParentId").Value,
                        Timestamp = (DateTime)a.Element("Timestamp"),
                        Url = a.Element("Url").Value.Trim(),
                        Visible = (bool)a.Element("Visible")
                    }).SingleOrDefault();
        }

        public List<MyAlbum.Model.Album> GetAlbumsBySectionId(string sectionId, bool visible)
        {
            return this.Document.Descendants("Album").Where(s => s.Element("ParentId").Value == sectionId && (visible ? (bool)s.Element("Visible") : true)).Select(s => new MyAlbum.Model.Album()
            {
                Id = s.Attribute("Id").Value,
                Name = s.Element("Name").Value.Trim(),
                ParentId = (s.Element("ParentId").Value),
                Timestamp = (DateTime)s.Element("Timestamp"),
                Visible = (bool)s.Element("Visible"),
                Url = s.Element("Url").Value.Trim(),
                Description = s.Element("Description").Value.Trim()
            }).ToList();
        }

    }
}
